package org.daochong.web.filter;

import java.util.Collections;
import java.util.Enumeration;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

public class SecurityParamRequestWarpper extends HttpServletRequestWrapper {
	private Map<String, String[]> params;
	private String queryString = null;

	public SecurityParamRequestWarpper(HttpServletRequest request) {
		super(request);
		Map<String, String[]> ar = request.getParameterMap();
		this.params = new LinkedHashMap<String, String[]>(ar.size());
		for (String key : ar.keySet()) {
			String[] olds = (String[]) ar.get(key);
			String[] vals = new String[olds.length];
			for (int i = 0; i < olds.length; i++) {
				vals[i] = SecurityParameterUtils.filter(olds[i]);
			}
			this.params.put(SecurityParameterUtils.filter(key), vals);
		}
		String str = request.getQueryString();
		if (str != null) {
			this.queryString = SecurityParameterUtils.processQueryString(str);
		}
	}

	public String getParameter(String name) {
		String[] ar = (String[]) this.params.get(name);
		return ar == null ? null : ar[0];
	}

	public Map<String, String[]> getParameterMap() {
		return this.params;
	}

	public String getQueryString() {
		return queryString;
	}

	public String getHeader(String name) {
		String value = super.getHeader(name);
		if (value != null) {
			value = SecurityParameterUtils.filter(value);
		}
		return value;
	}

	public Enumeration<String> getParameterNames() {
		return Collections.enumeration(this.params.keySet());
	}

	public String[] getParameterValues(String name) {
		return (String[]) this.params.get(name);
	}
}